'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 黑市购买
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var moment = require('moment');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var roleService = require('../../../services/roleService');
var rewardService = require('../../../services/rewardService');
var PlayerManager = require('../../../libs/playerManager');
var constantUtil = require('../../../util/constantUtil');
var blackMarketConf = require('../../../config/blackMarketConf.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 黑市购买
 */
Handler.prototype.buy = function (msg, session, next) {
    var role_id = session.uid;
    var shop_id = msg.shop_id;
    var player_manager = new PlayerManager(role_id);
    var reward_list = [];

    async.waterfall(
        [

            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'copy', 'bag', 'build', 'soldier', 'accrued'],
                    cb);
            },
            function (cb) {
                var role_model = player_manager.role;
                var copy_model = player_manager.copy;

                // 是否开启黑市
                var now_time = moment().unix();
                var end_time = copy_model.is_end_time;
                if (end_time <= now_time) {
                    return next(null, {
                        code: Code.BLACK_NOT_OPEN
                    });
                }

                // 加入购买记录商品id
                if (!copy_model.addBuyId(shop_id)) {
                    return next(null, {
                        code: Code.BLACK_BUY_SHOP_ID_OK_SAVE
                    });
                }

                reward_list = blackMarketConf.getConf(shop_id);
                var sub_consume = reward_list.discount_price;
                if (reward_list.cost_type == 2) {
                    if (!roleService.subDiamond(player_manager, moduleType.MDL_COPY, operationType.OPR_COPY_BLACKMARKET, sub_consume)) {
                        // 钻石够不够
                        return next(null, {
                            code: Code.USER_DIAMOND_NOT_ENOUGH
                        });
                    }

                    //  发放奖励
                    _rewardList(player_manager, reward_list);
                }
                if (reward_list.cost_type == 12) {
                    if (!roleService.subFood(player_manager, moduleType.MDL_COPY, operationType.OPR_COPY_BLACKMARKET, sub_consume)) {
                        return next(null, {
                            code: Code.USER_FOOD_NOT_ENOUGH
                        });
                    }

                    //  发放奖励
                    _rewardList(player_manager, reward_list);
                }
                if (reward_list.cost_type == 1) {
                    if (!roleService.subGold(player_manager, moduleType.MDL_COPY, operationType.OPR_COPY_BLACKMARKET, sub_consume)) {
                        return next(null, {
                            code: Code.USER_GOLD_NOT_ENOUGH
                        });
                    }

                    //  发放奖励
                    _rewardList(player_manager, reward_list);
                }

                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    reward_list: [reward_list]
                }
            });
        }
    );
};

// 资源接口
var _rewardList = function (player_manager, reward_list) {
    rewardService.addRewardListToRole(player_manager, [reward_list], moduleType.MDL_COPY, operationType.OPR_COPY_BLACKMARKET);
};
